import json
import requests
from frame.bases.base_common import BaseCommon
from frame.utils.log_utils import logger


class BaseAdmin(BaseCommon):
    """
    定义admin基类
    """
    __BaseAdminUrl = "https://litemall.hogwarts.ceshiren.com/admin/"

    def __init__(self):
        data = {
            "username": "admin123",
            "password": "admin123"
        }
        req = {
            "url":self.__BaseAdminUrl + "auth/login",
            "method":"post",
            "json":data
        }
        r = self.do_send(req)
        self.token = r.json().get('data').get('token')

    def send_api(self, req):
        if req.get('headers'):
            req['headers']['X-Litemall-Admin-Token'] = self.token
        else:
            req['headers'] = {"X-Litemall-Admin-Token": self.token}
        url = req['url']
        req['url'] = self.__BaseAdminUrl + url
        print(req['url'])

        r = self.do_send(req)
        logger.info(f"{url}接口的响应信息为:{json.dumps(r.json(), indent=2, ensure_ascii=False)}")

        return r
